
 //This file is part of Proactive Investigator
 //   Pro-I Investigator is free software: you can redistribute it and/or modify
 //   it under the terms of the GNU General Public License as published by
 //   the Free Software Foundation, either version 2 of the License, or
 //   (at your option) any later version.

 //   Proactive Investigator is distributed in the hope that it will be useful,
 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //   GNU General Public License for more details.

 //   You should have received a copy of the GNU General Public License
//   along with Pro-I Investigator.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;



namespace PROI._3D
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : Window
    {


        private static bool _opened = false;
        //neighborhood model objects
        Viewport3D myViewport = new Viewport3D();       
        PerspectiveCamera myPCamera = new PerspectiveCamera();
        DirectionalLight myDLight = new DirectionalLight();
        AmbientLight myAmbLight = new AmbientLight();
        MaterialGroup myMaterials = new MaterialGroup();
        Transform3DGroup allModelsTransformGroup = new Transform3DGroup();
        Model3DGroup allModels = new Model3DGroup();
        Model3DGroup pushpinModels = new Model3DGroup();
        ModelVisual3D pushpinVisual = new ModelVisual3D();
        // galaxy model objects
        Viewport3D GalaxyViewport = new Viewport3D();
        PerspectiveCamera GalaxyPCamera = new PerspectiveCamera();
        DirectionalLight GalaxyDLight = new DirectionalLight();
        AmbientLight GalaxyAmbLight = new AmbientLight();
        MaterialGroup GalaxyMaterials = new MaterialGroup();
        Transform3DGroup allGalaxyModelsTransformGroup = new Transform3DGroup();
        Model3DGroup allGalaxyModels = new Model3DGroup();
        //common objects
        GeometryModel3D side1 = new GeometryModel3D();
        GeometryModel3D side2 = new GeometryModel3D();
        GeometryModel3D side3 = new GeometryModel3D();
        GeometryModel3D side4 = new GeometryModel3D();
        GeometryModel3D side5 = new GeometryModel3D();
        GeometryModel3D side6 = new GeometryModel3D();
        GeometryModel3D side7 = new GeometryModel3D();
        GeometryModel3D side8 = new GeometryModel3D();
        GeometryModel3D side9 = new GeometryModel3D();
        GeometryModel3D side10 = new GeometryModel3D();
        GeometryModel3D side11 = new GeometryModel3D();
        GeometryModel3D side12 = new GeometryModel3D();
        GeometryModel3D side13 = new GeometryModel3D();
        GeometryModel3D side14 = new GeometryModel3D();
        GeometryModel3D side15 = new GeometryModel3D();
        GeometryModel3D side16 = new GeometryModel3D();
        GeometryModel3D side17 = new GeometryModel3D();
        GeometryModel3D side18 = new GeometryModel3D();
        GeometryModel3D side19 = new GeometryModel3D();
        GeometryModel3D side20 = new GeometryModel3D();
        GeometryModel3D side21 = new GeometryModel3D();
        GeometryModel3D side22 = new GeometryModel3D();
        GeometryModel3D side23 = new GeometryModel3D();
        GeometryModel3D side24 = new GeometryModel3D();
        GeometryModel3D side25 = new GeometryModel3D();
        GeometryModel3D side26 = new GeometryModel3D();
        GeometryModel3D side27 = new GeometryModel3D();
        GeometryModel3D side28 = new GeometryModel3D();
        GeometryModel3D side29 = new GeometryModel3D();
        GeometryModel3D side30 = new GeometryModel3D();
        MeshGeometry3D side1Plane = new MeshGeometry3D();
        MeshGeometry3D side2Plane = new MeshGeometry3D();
        MeshGeometry3D side3Plane = new MeshGeometry3D();
        MeshGeometry3D side4Plane = new MeshGeometry3D();
        MeshGeometry3D side5Plane = new MeshGeometry3D();
        MeshGeometry3D side6Plane = new MeshGeometry3D();
        DiffuseMaterial side1Material = new DiffuseMaterial();
        DiffuseMaterial side2Material = new DiffuseMaterial();
        DiffuseMaterial side3Material = new DiffuseMaterial();
        DiffuseMaterial side4Material = new DiffuseMaterial();
        DiffuseMaterial side5Material = new DiffuseMaterial();
        DiffuseMaterial side6Material = new DiffuseMaterial();

        Polyline modelPath = new Polyline();
        

        static double _sidelen = 0.0300;
        static double _xspacing = 0.120;
        static double _yspacing = 0.120;
        static double _zspacing = -0.50;
        static int _numsides = 6;
        static int _xunits = 6;
        static int _yunits = 5;
        static int _zunits = 0;
        static int _vertices = _xunits * _yunits * _zunits;
        static int _rotateidx;
        static int _series;
        //camera state
        RotateTransform3D myRotateTransform;
       Transform3DGroup transformCameraGroup;
        TranslateTransform3D cameraLookTransform;
        Point3D galcampos;
        Vector3D galcamlook;
        Point3D campos;
        Vector3D camlook;
        static int _endsecs = 15000;
        static int _acrosssecs = 7000;

        IDictionary<int, string> hudMapList = new Dictionary<int, string>();
        IDictionary<int, string> hudValList = new Dictionary<int, string>();
        IDictionary<string, int> hudList = new Dictionary<string, int>();
        IList<GeometryModel3D> sideArray = new List<GeometryModel3D>();
        IList<TranslateTransform3D> translateList = new List<TranslateTransform3D>();
        IList<Transform3DGroup> transformGroupList;
        IList<Transform3DGroup> pushpinTransformGroupList;
        IList<Model3DGroup> modelGroupList;
        IList<ModelUIElement3D> modelUIList;
        IList<Model3DGroup> pushpinGroupList;
        IList<ModelUIElement3D> pushpinUIList;
        //common lists
        IList<MeshGeometry3D> meshGeoList = new List<MeshGeometry3D>();
        ContainerUIElement3D containerUI = new ContainerUIElement3D();
        // galaxy lists
        RotateTransform3D GalaxyRotateTransform;
        IList<TranslateTransform3D> translateGalaxyList = new List<TranslateTransform3D>();
        IList<Transform3DGroup> transformGalaxyGroupList;
        IList<Model3DGroup> modelGalaxyGroupList;
        IList<ModelUIElement3D> modelGalaxyUIList;
        ContainerUIElement3D containerGalaxyUI = new ContainerUIElement3D();
        // data set variables
        static int idx;
        static byte view;
        IList<double[]> dataSetArray = new List<double[]>();
        IList<double> sumrow = new List<double>();
        static DateTime? startDate = DateTime.Parse("9/18/2011");
        static DateTime? endDate = DateTime.Parse("9/19/2011");

        IList<TextBox> labelList = new List<TextBox>(); 
        IList<TextBox> textboxList = new List<TextBox>();
        // case management objects

        // parallel query args
        IDictionary<string,string> argsPq = new Dictionary<string,string>();
        static string _workerId;
        static string _email;
        static string _source;
        //drilldown tab vars


        public Window1()
        {
            InitializeComponent();


        }
        
        private void WindowLoaded(object sender, EventArgs e)
        {

           DrawMeshes();

        
            
            
        }

  

        private void DrawSomeModels() 
        {
            if (grid2.Children.Count > 1)
            {
                myViewport.Children.Remove(pushpinVisual);
                grid2.Children.Remove(myViewport);
            }
            myViewport = new Viewport3D();
            myViewport.Name = "myViewport";
            myViewport.MouseRightButtonUp += new MouseButtonEventHandler(grid2_MouseRightButtonDown);
            
            allModels = new Model3DGroup();
            ModelVisual3D myModelVisual = new ModelVisual3D();
            transformGroupList = new List<Transform3DGroup>();
            transformCameraGroup = new Transform3DGroup();
            modelGroupList = new List<Model3DGroup>();
            modelUIList = new List<ModelUIElement3D>();
            pushpinGroupList = new List<Model3DGroup>();
            pushpinTransformGroupList = new List<Transform3DGroup>();
            pushpinUIList = new List<ModelUIElement3D>();

            //Define lights and cameras
            myPCamera.FarPlaneDistance = 100;
            myPCamera.NearPlaneDistance = 0;
            myPCamera.FieldOfView = 45;

            myPCamera.Position = new Point3D(-10, 5, 3);
            Point3D focal = new Point3D(0, 0,-2.8);
            myPCamera.LookDirection = new Vector3D(focal.X - myPCamera.Position.X, focal.Y - myPCamera.Position.Y, focal.Z - myPCamera.Position.Z);
            campos = myPCamera.Position;
            camlook = myPCamera.LookDirection;
            myPCamera.UpDirection = new Vector3D(0, 0.2, 0);
            // add animation transforms to camera
            cameraLookTransform = new TranslateTransform3D(new Vector3D(0.0,0.0,0.0));
            transformCameraGroup.Children.Add(cameraLookTransform);
            myPCamera.Transform = transformCameraGroup;


            myDLight.Color = Colors.White;
            myDLight.Direction = new Vector3D(-3, -4, -5);

            myAmbLight.Color = Colors.White;

            

            //set Geometry property of MeshGeometry3D
            side1.Geometry = meshGeoList[0];
            side2.Geometry = meshGeoList[1];
            side3.Geometry = meshGeoList[2];
            side4.Geometry = meshGeoList[3];
            side5.Geometry = meshGeoList[4];
            side6.Geometry = meshGeoList[5];
            side7.Geometry = meshGeoList[0];
            side8.Geometry = meshGeoList[1];
            side9.Geometry = meshGeoList[2];
            side10.Geometry = meshGeoList[3];
            side11.Geometry = meshGeoList[4];
            side12.Geometry = meshGeoList[5];
            side13.Geometry = meshGeoList[0];
            side14.Geometry = meshGeoList[1];
            side15.Geometry = meshGeoList[2];
            side16.Geometry = meshGeoList[3];
            side17.Geometry = meshGeoList[4];
            side18.Geometry = meshGeoList[5];
            side19.Geometry = meshGeoList[0];
            side20.Geometry = meshGeoList[1];
            side21.Geometry = meshGeoList[2];
            side22.Geometry = meshGeoList[3];
            side23.Geometry = meshGeoList[4];
            side24.Geometry = meshGeoList[5];
            side25.Geometry = meshGeoList[0];
            side26.Geometry = meshGeoList[1];
            side27.Geometry = meshGeoList[2];
            side28.Geometry = meshGeoList[3];
            side29.Geometry = meshGeoList[4];
            side30.Geometry = meshGeoList[5];
            _vertices = _xunits * _yunits * _zunits;
            try
            {
                // create model3dgroups
                for (int i = 0; i <= _vertices - 1; i++)
                {
                    modelGroupList.Add(new Model3DGroup());
                    transformGroupList.Add(new Transform3DGroup());
                    modelUIList.Add(new ModelUIElement3D());
                }
            }
            catch (Exception ex)
            {


            }

            
            

            //create translations
            try
            {
                for (int i = 0; i <= _vertices - 1; i++)
                {
                    //transformGroupList[i].Children.Add(new TranslateTransform3D(new Vector3D((i % _xunits) * _xspacing, ((i/_xunits)%_xunits) * _yspacing, i / (_xunits * _yunits) * _zspacing)));
                    transformGroupList[i].Children.Add(new TranslateTransform3D((i % _xunits) * _xspacing, ((i / _xunits) % _yunits) * _yspacing, i / (_xunits * _yunits) * _zspacing));
                    transformGroupList[i].Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 0)));
                    transformGroupList[i].Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0)));
                    transformGroupList[i].Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 0)));
                    transformGroupList[i].Children.Add(new ScaleTransform3D(1.0, 1.0, 1.0));

                }
            }
            catch (Exception ex)
            {

            }
            
            //RotateTransform3D myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 0.25), 0.5));
            //Define an animation for the rotation
            //DoubleAnimation myAnimation = new DoubleAnimation();
            //myAnimation.From = 1;
            //myAnimation.To = 361;
            //myAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(20000));
            //myAnimation.RepeatBehavior = RepeatBehavior.Forever;

            //Define another animation
            //Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(-1, -1, -1), new Duration(TimeSpan.FromMilliseconds(5000)));
            //myVectorAnimation.RepeatBehavior = RepeatBehavior.Forever;

            
            //myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, myAnimation);
            //myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AxisProperty, myVectorAnimation);
            //NeighborhoodLoadStatus.Value = 1;
            
            
            //Add transformation to the model            
            try
            {
                for (int i = 0; i <= _vertices - 1; i++)
                {

                    AddSidesToGroup(i);
                    modelUIList[i].Model = modelGroupList[i];
                    modelUIList[i].Transform = transformGroupList[i];
                    modelUIList[i].MouseLeftButtonDown += new MouseButtonEventHandler(OnModelMouseDown);
                    modelUIList[i].MouseEnter += new MouseEventHandler(OnModelMouseOver);
                    modelUIList[i].MouseRightButtonUp += new MouseButtonEventHandler(grid2_MouseRightButtonDown); 
                    allModels.Children.Add(modelGroupList[i]);
 

                }
            }
            catch (Exception ex)
            {

            }

            for (int i=0; i < modelUIList.Count; i++)
            {
               myViewport.Children.Add(modelUIList[i]);
            }
            allModels.Children.Add(myAmbLight);
            myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 0), 0));
            allModels.Transform = myRotateTransform;
            

            myViewport.Camera = myPCamera;
            myModelVisual.Content = allModels;
            //myModelVisual.Content = null;
            
            
            myViewport.Children.Add(myModelVisual);
            myViewport.Children.Add(pushpinVisual);


            //mainWindow.Content = myViewport;
            grid2.Children.Add(myViewport);  
            
            
        }

        private void DrawMeshes()
        {

                meshGeoList.Add(new MeshGeometry3D());
                meshGeoList[0].Positions.Add(new Point3D(-_sidelen, -_sidelen, -_sidelen));
                meshGeoList[0].Positions.Add(new Point3D(-_sidelen, _sidelen, -_sidelen));
                meshGeoList[0].Positions.Add(new Point3D(_sidelen, _sidelen, -_sidelen));
                meshGeoList[0].Positions.Add(new Point3D(_sidelen, _sidelen, -_sidelen));
                meshGeoList[0].Positions.Add(new Point3D(_sidelen, -_sidelen, -_sidelen));
                meshGeoList[0].Positions.Add(new Point3D(-_sidelen, -_sidelen, -_sidelen));

                meshGeoList[0].TriangleIndices.Add(0);
                meshGeoList[0].TriangleIndices.Add(1);
                meshGeoList[0].TriangleIndices.Add(2);
                meshGeoList[0].TriangleIndices.Add(3);
                meshGeoList[0].TriangleIndices.Add(4);
                meshGeoList[0].TriangleIndices.Add(5);

                meshGeoList[0].Normals.Add(new Vector3D(0, 0, -1));
                meshGeoList[0].Normals.Add(new Vector3D(0, 0, -1));
                meshGeoList[0].Normals.Add(new Vector3D(0, 0, -1));
                meshGeoList[0].Normals.Add(new Vector3D(0, 0, -1));
                meshGeoList[0].Normals.Add(new Vector3D(0, 0, -1));
                meshGeoList[0].Normals.Add(new Vector3D(0, 0, -1));

                meshGeoList[0].TextureCoordinates.Add(new Point(1, 0));
                meshGeoList[0].TextureCoordinates.Add(new Point(1, 1));
                meshGeoList[0].TextureCoordinates.Add(new Point(0, 1));
                meshGeoList[0].TextureCoordinates.Add(new Point(0, 1));
                meshGeoList[0].TextureCoordinates.Add(new Point(0, 0));
                meshGeoList[0].TextureCoordinates.Add(new Point(1, 0));

            
            
            ////side1-------------------------------------------------

            ////side2-------------------------------------------------
                meshGeoList.Add(new MeshGeometry3D()); 
                meshGeoList[1].Positions.Add(new Point3D(-_sidelen, -_sidelen, _sidelen));
                meshGeoList[1].Positions.Add(new Point3D(_sidelen, -_sidelen, _sidelen));
                meshGeoList[1].Positions.Add(new Point3D(_sidelen, _sidelen, _sidelen));
                meshGeoList[1].Positions.Add(new Point3D(_sidelen, _sidelen, _sidelen));
                meshGeoList[1].Positions.Add(new Point3D(-_sidelen, _sidelen, _sidelen));
                meshGeoList[1].Positions.Add(new Point3D(-_sidelen, -_sidelen, _sidelen));

                meshGeoList[1].TriangleIndices.Add(0);
                meshGeoList[1].TriangleIndices.Add(1);
                meshGeoList[1].TriangleIndices.Add(2);
                meshGeoList[1].TriangleIndices.Add(3);
                meshGeoList[1].TriangleIndices.Add(4);
                meshGeoList[1].TriangleIndices.Add(5);

                meshGeoList[1].Normals.Add(new Vector3D(0, 0, 1));
                meshGeoList[1].Normals.Add(new Vector3D(0, 0, 1));
                meshGeoList[1].Normals.Add(new Vector3D(0, 0, 1));
                meshGeoList[1].Normals.Add(new Vector3D(0, 0, 1));
                meshGeoList[1].Normals.Add(new Vector3D(0, 0, 1));
                meshGeoList[1].Normals.Add(new Vector3D(0, 0, 1));

                meshGeoList[1].TextureCoordinates.Add(new Point(0, 0));
                meshGeoList[1].TextureCoordinates.Add(new Point(1, 0));
                meshGeoList[1].TextureCoordinates.Add(new Point(1, 1));
                meshGeoList[1].TextureCoordinates.Add(new Point(1, 1));
                meshGeoList[1].TextureCoordinates.Add(new Point(0, 1));
                meshGeoList[1].TextureCoordinates.Add(new Point(0, 0));

            ////side3-------------------------------------------------
                meshGeoList.Add(new MeshGeometry3D()); 
                meshGeoList[2].Positions.Add(new Point3D(-_sidelen, -_sidelen, -_sidelen));
                meshGeoList[2].Positions.Add(new Point3D(_sidelen, -_sidelen, -_sidelen));
                meshGeoList[2].Positions.Add(new Point3D(_sidelen, -_sidelen, _sidelen));
                meshGeoList[2].Positions.Add(new Point3D(_sidelen, -_sidelen, _sidelen));
                meshGeoList[2].Positions.Add(new Point3D(-_sidelen, -_sidelen, _sidelen));
                meshGeoList[2].Positions.Add(new Point3D(-_sidelen, -_sidelen, -_sidelen));

                meshGeoList[2].TriangleIndices.Add(0);
                meshGeoList[2].TriangleIndices.Add(1);
                meshGeoList[2].TriangleIndices.Add(2);
                meshGeoList[2].TriangleIndices.Add(3);
                meshGeoList[2].TriangleIndices.Add(4);
                meshGeoList[2].TriangleIndices.Add(5);

                meshGeoList[2].Normals.Add(new Vector3D(0, -1, 0));
                meshGeoList[2].Normals.Add(new Vector3D(0, -1, 0));
                meshGeoList[2].Normals.Add(new Vector3D(0, -1, 0));
                meshGeoList[2].Normals.Add(new Vector3D(0, -1, 0));
                meshGeoList[2].Normals.Add(new Vector3D(0, -1, 0));
                meshGeoList[2].Normals.Add(new Vector3D(0, -1, 0));

                meshGeoList[2].TextureCoordinates.Add(new Point(0, 0));
                meshGeoList[2].TextureCoordinates.Add(new Point(1, 0));
                meshGeoList[2].TextureCoordinates.Add(new Point(1, 1));
                meshGeoList[2].TextureCoordinates.Add(new Point(1, 1));
                meshGeoList[2].TextureCoordinates.Add(new Point(0, 1));
                meshGeoList[2].TextureCoordinates.Add(new Point(0, 0));

            ////side4-------------------------------------------------
                meshGeoList.Add(new MeshGeometry3D()); 
                meshGeoList[3].Positions.Add(new Point3D(_sidelen, -_sidelen, -_sidelen));
                meshGeoList[3].Positions.Add(new Point3D(_sidelen, _sidelen, -_sidelen));
                meshGeoList[3].Positions.Add(new Point3D(_sidelen, _sidelen, _sidelen));
                meshGeoList[3].Positions.Add(new Point3D(_sidelen, _sidelen, _sidelen));
                meshGeoList[3].Positions.Add(new Point3D(_sidelen, -_sidelen, _sidelen));
                meshGeoList[3].Positions.Add(new Point3D(_sidelen, -_sidelen, -_sidelen));

                meshGeoList[3].TriangleIndices.Add(0);
                meshGeoList[3].TriangleIndices.Add(1);
                meshGeoList[3].TriangleIndices.Add(2);
                meshGeoList[3].TriangleIndices.Add(3);
                meshGeoList[3].TriangleIndices.Add(4);
                meshGeoList[3].TriangleIndices.Add(5);

                meshGeoList[3].Normals.Add(new Vector3D(1, 0, 0));
                meshGeoList[3].Normals.Add(new Vector3D(1, 0, 0));
                meshGeoList[3].Normals.Add(new Vector3D(1, 0, 0));
                meshGeoList[3].Normals.Add(new Vector3D(1, 0, 0));
                meshGeoList[3].Normals.Add(new Vector3D(1, 0, 0));
                meshGeoList[3].Normals.Add(new Vector3D(1, 0, 0));

                meshGeoList[3].TextureCoordinates.Add(new Point(1, 0));
                meshGeoList[3].TextureCoordinates.Add(new Point(1, 1));
                meshGeoList[3].TextureCoordinates.Add(new Point(0, 1));
                meshGeoList[3].TextureCoordinates.Add(new Point(0, 1));
                meshGeoList[3].TextureCoordinates.Add(new Point(0, 0));
                meshGeoList[3].TextureCoordinates.Add(new Point(1, 0));

            ////side5-------------------------------------------------
                meshGeoList.Add(new MeshGeometry3D()); 
                meshGeoList[4].Positions.Add(new Point3D(_sidelen, _sidelen, -_sidelen));
                meshGeoList[4].Positions.Add(new Point3D(-_sidelen, _sidelen, -_sidelen));
                meshGeoList[4].Positions.Add(new Point3D(-_sidelen, _sidelen, _sidelen));
                meshGeoList[4].Positions.Add(new Point3D(-_sidelen, _sidelen, _sidelen));
                meshGeoList[4].Positions.Add(new Point3D(_sidelen, _sidelen, _sidelen));
                meshGeoList[4].Positions.Add(new Point3D(_sidelen, _sidelen, -_sidelen));

                meshGeoList[4].TriangleIndices.Add(0);
                meshGeoList[4].TriangleIndices.Add(1);
                meshGeoList[4].TriangleIndices.Add(2);
                meshGeoList[4].TriangleIndices.Add(3);
                meshGeoList[4].TriangleIndices.Add(4);
                meshGeoList[4].TriangleIndices.Add(5);

                meshGeoList[4].Normals.Add(new Vector3D(0, 1, 0));
                meshGeoList[4].Normals.Add(new Vector3D(0, 1, 0));
                meshGeoList[4].Normals.Add(new Vector3D(0, 1, 0));
                meshGeoList[4].Normals.Add(new Vector3D(0, 1, 0));
                meshGeoList[4].Normals.Add(new Vector3D(0, 1, 0));
                meshGeoList[4].Normals.Add(new Vector3D(0, 1, 0));

                meshGeoList[4].TextureCoordinates.Add(new Point(1, 1));
                meshGeoList[4].TextureCoordinates.Add(new Point(0, 1));
                meshGeoList[4].TextureCoordinates.Add(new Point(0, 0));
                meshGeoList[4].TextureCoordinates.Add(new Point(0, 0));
                meshGeoList[4].TextureCoordinates.Add(new Point(1, 0));
                meshGeoList[4].TextureCoordinates.Add(new Point(1, 1));

            ////side6-------------------------------------------------
                meshGeoList.Add(new MeshGeometry3D()); 
                meshGeoList[5].Positions.Add(new Point3D(-_sidelen, _sidelen, -_sidelen));
                meshGeoList[5].Positions.Add(new Point3D(-_sidelen, -_sidelen, -_sidelen));
                meshGeoList[5].Positions.Add(new Point3D(-_sidelen, -_sidelen, _sidelen));
                meshGeoList[5].Positions.Add(new Point3D(-_sidelen, -_sidelen, _sidelen));
                meshGeoList[5].Positions.Add(new Point3D(-_sidelen, _sidelen, _sidelen));
                meshGeoList[5].Positions.Add(new Point3D(-_sidelen, _sidelen, -_sidelen));

                meshGeoList[5].TriangleIndices.Add(0);
                meshGeoList[5].TriangleIndices.Add(1);
                meshGeoList[5].TriangleIndices.Add(2);
                meshGeoList[5].TriangleIndices.Add(3);
                meshGeoList[5].TriangleIndices.Add(4);
                meshGeoList[5].TriangleIndices.Add(5);

                meshGeoList[5].Normals.Add(new Vector3D(-1, 0, 0));
                meshGeoList[5].Normals.Add(new Vector3D(-1, 0, 0));
                meshGeoList[5].Normals.Add(new Vector3D(-1, 0, 0));
                meshGeoList[5].Normals.Add(new Vector3D(-1, 0, 0));
                meshGeoList[5].Normals.Add(new Vector3D(-1, 0, 0));
                meshGeoList[5].Normals.Add(new Vector3D(-1, 0, 0));

                meshGeoList[5].TextureCoordinates.Add(new Point(0, 1));
                meshGeoList[5].TextureCoordinates.Add(new Point(0, 0));
                meshGeoList[5].TextureCoordinates.Add(new Point(1, 0));
                meshGeoList[5].TextureCoordinates.Add(new Point(1, 0));
                meshGeoList[5].TextureCoordinates.Add(new Point(1, 1));
                meshGeoList[5].TextureCoordinates.Add(new Point(0, 1));


            //Set Brush property for the Material applied to each face
                DiffuseMaterial side2Material = new DiffuseMaterial((new SolidColorBrush(Colors.Yellow)));
            //side2Material.Color = Color.FromRgb(255, 255, 0);
                DiffuseMaterial side6Material = new DiffuseMaterial((new SolidColorBrush(Colors.Green)));
            DiffuseMaterial side1Material = new DiffuseMaterial((new SolidColorBrush(Colors.Orange)));
            //side1Material.Color = Color.FromRgb(0, 0, 0);
            DiffuseMaterial side3Material = new DiffuseMaterial((new SolidColorBrush(Colors.Red)));
            //side3Material.Color = Color.FromRgb(255, 0, 0);
            DiffuseMaterial side4Material = new DiffuseMaterial((new SolidColorBrush(Colors.MediumPurple)));
            DiffuseMaterial side5Material = new DiffuseMaterial((new SolidColorBrush(Colors.Blue)));

            //yellow cube
            side1.Material = side2Material;            
            side2.Material = side2Material;
            side3.Material = side2Material;
            side4.Material = side2Material;
            side5.Material = side2Material;
            side6.Material = side2Material;
            // red cube
            side7.Material = side3Material;
            side8.Material = side3Material;
            side9.Material = side3Material;
            side10.Material = side3Material;
            side11.Material = side3Material;
            side12.Material = side3Material;
            //purple cube
            side13.Material = side4Material;
            side14.Material = side4Material;
            side15.Material = side4Material;
            side16.Material = side4Material;
            side17.Material = side4Material;
            side18.Material = side4Material;
            //blue cube
            side19.Material = side5Material;
            side20.Material = side5Material;
            side21.Material = side5Material;
            side22.Material = side5Material;
            side23.Material = side5Material;
            side24.Material = side5Material;
            //green cube
            side25.Material = side6Material;
            side26.Material = side6Material;
            side27.Material = side6Material;
            side28.Material = side6Material;
            side29.Material = side6Material;
            side30.Material = side6Material;

            sideArray.Add(side1);
            sideArray.Add(side2);
            sideArray.Add(side3);
            sideArray.Add(side4);
            sideArray.Add(side5);
            sideArray.Add(side6);
            sideArray.Add(side7);
            sideArray.Add(side8);
            sideArray.Add(side9);
            sideArray.Add(side10);
            sideArray.Add(side11);
            sideArray.Add(side12);
            sideArray.Add(side13);
            sideArray.Add(side14);
            sideArray.Add(side15);
            sideArray.Add(side16);
            sideArray.Add(side17);
            sideArray.Add(side18);
            sideArray.Add(side19);
            sideArray.Add(side20);
            sideArray.Add(side21);
            sideArray.Add(side22);
            sideArray.Add(side23);
            sideArray.Add(side24);
            sideArray.Add(side25);
            sideArray.Add(side26);
            sideArray.Add(side27);
            sideArray.Add(side28);
            sideArray.Add(side29);
            sideArray.Add(side30);

        }

        private void AddSidesToGroup(int idx)
        {
            //logic to decide which color sides to add
            //dataSetArray[i%(_xunits,_yunits)]
            //Random rand = new Random();
            //int color = rand.Next(1, 4);
            //int color = cubemodel
            double dataItem = dataSetArray[idx / (_xunits * _yunits)][idx % (_xunits * _yunits)];
            if (dataItem >= 1000000.0 | (idx / (_xunits * _yunits) == 0))
            {
                // blue box
                for (int i = _numsides * 3; i <= _numsides * 4 - 1; i++)
                {
                    modelGroupList[idx].Children.Add(sideArray[i]);
                }
            }
            else if (dataItem == 100.0)
            {
                //purple box
                for (int i = _numsides * 2; i <= _numsides * 3 - 1; i++)
                {
                    modelGroupList[idx].Children.Add(sideArray[i]);
                }
            }
            else if (dataItem < 1000000.0 & dataItem >= 3.0)
            {
                //red box
                for (int i = _numsides; i <= _numsides * 2 - 1; i++)
                {
                    modelGroupList[idx].Children.Add(sideArray[i]);
                }
            }
            else if (dataItem < 3.0 & dataItem >= 2.0)
            {
                // yellow box
                for (int i = 0; i <= _numsides - 1; i++)
                {
                    modelGroupList[idx].Children.Add(sideArray[i]);
                }
            }

            else
            {
                //for (int i = _numsides * 2; i <= _numsides * 3 - 1; i++)
                //{
                //    modelGroupList[idx].Children.Add(sideArray[i]);
                //}

            }
            
        }

        /// <summary>
        /// On mouse click, return data on the specific model
        /// where the click happened.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void OnModelMouseDown(object sender, System.Windows.Input.MouseEventArgs e)
        {
            //e.Handled = true;
            // get ID from the model

            idx = (myViewport.Children.IndexOf((ModelUIElement3D)sender))/(_xunits*_yunits);

            if (view == 0)
            {
                _workerId = dataSetArray[idx][0].ToString();

            }
            if (view == 1)
            {
                startDatePicker.SelectedDate = Convert.ToDateTime(dataSetArray[idx][0].ToString().Substring(4, 2) + "/" + dataSetArray[idx][0].ToString().Substring(6, 2) + "/" + dataSetArray[idx][0].ToString().Substring(0, 4));
            }
            #region hudpanel
            // call up HUD in panel
            wrapPanel1.Children.Clear();
            int _labelwidth = 70;
            //headers
            TextBox lbl = new TextBox();
            lbl.Text = "Worker";
            lbl.Width = _labelwidth;
            lbl.FontWeight = FontWeights.ExtraBold;
            wrapPanel1.Children.Add(lbl);
            TextBox lbl0 = new TextBox();
            lbl0.Text = "IP1"; //
            lbl0.Width = _labelwidth;
            lbl0.FontWeight = FontWeights.ExtraBold;
            wrapPanel1.Children.Add(lbl0);
            TextBox lbl1 = new TextBox();
            lbl1.Text = "IP2"; //
            lbl1.Width = _labelwidth;
            lbl1.FontWeight = FontWeights.ExtraBold;
            wrapPanel1.Children.Add(lbl1);
            TextBox lbl2 = new TextBox();
            lbl2.Text = "IP3"; //
            lbl2.Width = _labelwidth;
            lbl2.FontWeight = FontWeights.ExtraBold; 
            wrapPanel1.Children.Add(lbl2);
            TextBox lbl3 = new TextBox(); 
            lbl3.Text = "VPN";
            lbl3.Width = _labelwidth;
            lbl3.FontWeight = FontWeights.ExtraBold;
            wrapPanel1.Children.Add(lbl3);
            TextBox lbl4 = new TextBox();
            lbl4.Text = "DC";
            lbl4.Width = _labelwidth;
            lbl4.FontWeight = FontWeights.ExtraBold;
            wrapPanel1.Children.Add(lbl4); 
            
            textboxList = new List<TextBox>();
            labelList = new List<TextBox>();


            for (int i = 0; i < hudMapList.Count - 5; i += _xunits)
            {
                for (int j = 0; j < _xunits; j++)
                {
                    labelList.Add(new TextBox());
                    labelList[i + j].Text = hudMapList[i + j];
                    labelList[i + j].Width = _labelwidth;
                    if (dataSetArray[idx][hudMapList.Count - _xunits - i + j] > 10000000.0)
                    {
                        labelList[i + j].BorderThickness = new Thickness(2.0);
                        labelList[i + j].BorderBrush = Brushes.Blue;
                    }
                    else if (dataSetArray[idx][hudMapList.Count - _xunits - i + j] >= 3.0)
                    {
                        labelList[i + j].BorderThickness = new Thickness(2.0);

                        labelList[i + j].BorderBrush = Brushes.DarkRed;
                    }
                    else if (dataSetArray[idx][hudMapList.Count - _xunits - i + j] >= 2.0)
                    {
                        labelList[i + j].BorderThickness = new Thickness(2.0);
                        labelList[i + j].BorderBrush = Brushes.Yellow;
                    }
                    else
                    { }

                    wrapPanel1.Children.Add(labelList[i + j]);      


                }
                for (int j = 0; j < _xunits; j++)
                {
                    textboxList.Add(new TextBox());
                    //if (i + j != 0)
                    //{
                        textboxList[i + j].Text = dataSetArray[idx][hudMapList.Count - _xunits - i + j].ToString();
                   // }
                    textboxList[i + j].Width = _labelwidth;
                    textboxList[i + j].MouseDoubleClick += new MouseButtonEventHandler(OnHudTextMouseDown);
                    if (i >= hudMapList.Count - hudValList.Count)
                    {
                        textboxList[i + j].ToolTip += hudValList[i + j] + ":" + dataSetArray[idx][i + j + hudValList.Count].ToString();
                    }
                    if (dataSetArray[idx][hudMapList.Count - _xunits - i + j] > 10000000.0)
                    {
                        textboxList[i + j].BorderThickness = new Thickness(2.0);
                        textboxList[i + j].BorderBrush = Brushes.Blue;
                    }
                    else if (dataSetArray[idx][hudMapList.Count - _xunits - i + j] >= 3.0)
                    {
                        textboxList[i + j].BorderThickness = new Thickness(2.0);
                        textboxList[i + j].BorderBrush = Brushes.DarkRed;
                    }
                    else if (dataSetArray[idx][hudMapList.Count - _xunits - i + j] >= 2.0)
                    {
                        textboxList[i + j].BorderThickness = new Thickness(2.0);
                        textboxList[i + j].BorderBrush = Brushes.Yellow;
                    }
                    else
                    { }
                    wrapPanel1.Children.Add(textboxList[i + j]);
                    
                }
            }
 
            #endregion
            startDate = startDatePicker.SelectedDate;
            endDate = endDatePicker.SelectedDate;   
        }

        /// <summary>
        /// On mouse over, return data on the specific model
        /// where the click happened.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void OnModelMouseOver(object sender, System.Windows.Input.MouseEventArgs e)
        {
            //e.Handled = true;
            // get ID from the model
            int idx = (myViewport.Children.IndexOf((ModelUIElement3D)sender));
            //if (idx % (_xunits * _yunits) != 0)
            //{
            StatusBlock.Text = hudMapList[(idx % (_xunits * _yunits)) + (_xunits * (_yunits - 1 + ((idx % (_xunits * _yunits)) / _xunits * -2)))] + ": " + dataSetArray[idx / (_xunits * _yunits)][idx % (_xunits * _yunits)].ToString();
            //}

        }

 

        void GetDataSet(byte dataview)
        {
            try
            {
                dataSetArray = new List<double[]>();
                sumrow = new List<double>();
                //first check to see what the last data of available data is
                string _command = "";
               switch (dataview)
                {
                    case 1:
                        _command = "SELECT Convert(varchar(10),[ReportDate],112) AS \"Worker_ID\"," +
                            //"SELECT CAST(DATEPART(yyyy,[ReportDate]) as varchar(4)) + CAST(DATEPART(mm,[ReportDate]) as varchar(2)) + CAST(DATEPART(dd,[ReportDate]) as varchar(2)) AS \"Worker_ID\"" +
                    "[Sys1OutPage] AS \"Sys1OutPage\",[Sys2OutPage] AS \"Sys2OutPage\",[Sys3OutPage] AS \"Sys3OutPage\",[VpnOutUl] AS \"VpnOutUl\",[DcOutAct] as \"DcOutAct\"," +
                    "[NTL]+[NonAct] AS \"NTLNonAct\",[Sys1OutDoc] AS \"Sys1OutDoc\",[Sys2OutDoc] AS \"Sys2OutDoc\",[Sys3OutDoc] as \"Sys3OutDoc\", [VpnOutDl] as \"VpnOutDl\",[DcOutSys] as \"DcOutSys\"," +
                    "[NonReg] + [NonTech] as \"NonRegNonTech\",[Sys1OutHrs] as \"Sys1OutHrs\" ,[Sys2OutHrs] as \"Sys2OutHrs\",[Sys3OutHrs] as \"Sys3OutHrs\",[VpnOutHrs] as \"VpnOutHrs\", [DcOutHrs] as \"DcOutHrs\"," +
                    "[SecInc] as \"SecInc\",[Sys1Unauth] as \"Sys1Unauth\",[Sys2Unauth] as \"Sys2Unauth\", [Sys3Unauth] as \"Sys3Unauth\", [VpnUnauth] as \"VpnUnauth\", [Dc Unauth] as \"DcUnauth\"," +
                    "[Admin] as \"Admin\", [Blank2] as \" \",[CC] as \"CC\", [Blank1] as \" \", [VpnOutGeo] as \"OutGeo\", [DcOutChg] as \"OutChg\"," +
                    "[NTL]+[NonAct] AS \"NTLNonActVal\", [Sys1AccessCt] as \"Sys1AccessCt\", [Sys2AccessCt] as \"Sys2AccessCt\",[Sys3AccessCt] as \"Sys3AccessCt\",[VpnUpload] as \"VpnUpload\",[DcAccessCt] as \"DcAccessCt\"," +
                    "[Worker_ID],[Sys1DnctCt] as \"Sys1DnctCt\",[Sys2DnctCt] as \"Sys2DnctCt\",[Sys2DnctCt] as \"Sys2DnctCt\",[Sys3DnctCt] as \"Sys3DnctCt\", [VpnDownload] as \"VpnDownload\", [DcSystemCt] as \"DcSystemCt\"" +
                " FROM [Demo_ProI_Reporting].[dbo].[demo_fact_indicator_matrix] WHERE ([Worker_ID]='" + _workerId + "') ORDER BY [ReportDate] DESC ";

                        break;
                    default:
                        _command = "SELECT [Worker_ID],[Sys1OutPage] AS \"Sys1OutPage\",[Sys2OutPage] AS \"Sys2OutPage\",[Sys3OutPage] AS \"Sys3OutPage\",[VpnOutUl] AS \"VpnOutUl\",[DcOutAct] as \"DcOutAct\"," +
                    "[NTL]+[NonAct] AS \"NTLNonAct\",[Sys1OutDoc] AS \"Sys1OutDoc\",[Sys2OutDoc] AS \"Sys2OutDoc\",[Sys3OutDoc] as \"Sys3OutDoc\", [VpnOutDl] as \"VpnOutDl\",[DcOutSys] as \"DcOutSys\"," +
                    "[NonReg] + [NonTech] as \"NonRegNonTech\",[Sys1OutHrs] as \"Sys1OutHrs\" ,[Sys2OutHrs] as \"Sys2OutHrs\",[Sys3OutHrs] as \"Sys3OutHrs\",[VpnOutHrs] as \"VpnOutHrs\", [DcOutHrs] as \"DcOutHrs\"," +
                    "[SecInc] as \"SecInc\",[Sys1Unauth] as \"Sys1Unauth\",[Sys2Unauth] as \"Sys2Unauth\", [Sys3Unauth] as \"Sys3Unauth\", [VpnUnauth] as \"VpnUnauth\", [Dc Unauth] as \"DcUnauth\"," +
                    "[Admin] as \"Admin\", [Blank2] as \" \",[CC] as \"CC\", [Blank1] as \" \", [VpnOutGeo] as \"OutGeo\", [DcOutChg] as \"OutChg\"," +
                    "[NTL]+[NonAct] AS \"NTLNonActVal\",[Sys1DnctCt] as \"Sys1DnctCt\",[Sys2DnctCt] as \"Sys2DnctCt\",[Sys3DnctCt] as \"Sys3DnctCt\", [VpnDownload] as \"VpnDownload\", [DcSystemCt] as \"DcSystemCt\"," +
                    "[Worker_ID], [Sys1AccessCt] as \"Sys1AccessCt\", [Sys2AccessCt] as \"Sys2AccessCt\",[Sys3AccessCt] as \"Sys3AccessCt\",[VpnUpload] as \"VpnUpload\",[DcAccessCt] as \"DcAccessCt\", 0 as \"Perf\" " +
                    " FROM [Demo_ProI_Reporting].[dbo].[demo_fact_indicator_matrix] WHERE ([ReportDate] between '" + startDate + "' AND '" + endDate + "') ORDER BY [NTL] DESC ";
                   break;
               }

                SetHudList();
                string _connection = "Data Source=" + ConfigurationManager.AppSettings["Database"].ToString() + ";Database='Demo_ProI_Reporting';" +
                "Persist Security Info=False;Integrated Security=SSPI; Connect Timeout=120;Min Pool Size = 10; Max Pool Size = 100";
                SqlConnection connection = new SqlConnection(_connection);
                SqlCommand _cmdDatabase = new SqlCommand();
                _cmdDatabase.Connection = connection;
                _cmdDatabase.CommandType = CommandType.Text;
                _cmdDatabase.CommandText = _command;
                _cmdDatabase.CommandTimeout = 120;
                //_cmdDatabase.Parameters.Add("@search_params", SqlDbType.VarChar);
                //_cmdDatabase.Parameters["@search_params"].Value = searchParams;
                //_cmdDatabase.Parameters.Add("@search_id", SqlDbType.Int);
                // _cmdDatabase.Parameters["@search_id"].Value = 0;
                // _cmdDatabase.Parameters["@search_id"].Direction = ParameterDirection.Output;
                connection.Open();
                SqlDataReader reader = _cmdDatabase.ExecuteReader();
                _xunits = reader.FieldCount;
                double[] blankrow = new double[_xunits];
                for (int i = 0; i < _xunits; i++)
                {
                    sumrow.Add(0);
                    blankrow[i] = 0.0;
                }
                dataSetArray.Add(blankrow);
                _zunits = 0;
                while (reader.Read())
                {
                    double[] row = new double[_xunits];
                    
                    for (int i = 0; i < _xunits; i++)
                    {
                        if (reader[i] != null & reader[i].ToString() != "")
                        {
                            row[i] = Convert.ToDouble(reader[i]);
                            sumrow[i] += row[i];
                        }
                        else
                        {
                            row[i] = 0.0;
                        }
                    }
                    dataSetArray.Add(row);
                    _zunits += 1;

                }
                connection.Close();
                _xunits = (_xunits-hudValList.Count)/_yunits;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            

        }

        private void SetHudList()
        {


            hudMapList.Clear();
            //hudMapList.Add(35, "Perf");
            hudMapList.Add(0, "Admin"); 
            hudMapList.Add(1, "");
            hudMapList.Add(2, "CC");
            hudMapList.Add(3, "");
            hudMapList.Add(4, "OutGeo");
            hudMapList.Add(5, "OutChg");
            hudMapList.Add(6, "SecInc");
            hudMapList.Add(7, "Unauth");
            hudMapList.Add(8, "Unauth");
            hudMapList.Add(9, "Unauth");
            hudMapList.Add(10, "VpnUnauth");
            hudMapList.Add(11, "Unauth");
            hudMapList.Add(12, "NonReg/NonTech");
            hudMapList.Add(13, "OutHrs"); 
            hudMapList.Add(14, "OutHrs");
            hudMapList.Add(15, "OutHrs");
            hudMapList.Add(16, "VpnOutHrs");
            hudMapList.Add(17, "OutHrs");
            hudMapList.Add(18, "LDO/NonAct");
            hudMapList.Add(19, "OutDoc"); 
            hudMapList.Add(20, "OutDoc");
            hudMapList.Add(21, "OutDoc");
            hudMapList.Add(22, "VpnOutDl");
            hudMapList.Add(23, "OutSys");
            hudMapList.Add(24, "ID");
            hudMapList.Add(25, "OutAccess");
            hudMapList.Add(26, "OutAccess");
            hudMapList.Add(27, "OutAccess");
            hudMapList.Add(28, "VpnOutUl");
            hudMapList.Add(29, "OutAct");

            hudValList.Clear();
            hudValList.Add(18,"LDO+NonAct");
            hudValList.Add(19, "Distinct Ct");
            hudValList.Add(20,"Distinct Ct");
            hudValList.Add(21,"Distinct Ct");
            hudValList.Add(22,"Upload");
            hudValList.Add(23,"System Ct");
            hudValList.Add(24,"ID");
            hudValList.Add(25, "Access Ct");
            hudValList.Add(26,"Access Ct");
            hudValList.Add(27,"Access Ct");
            hudValList.Add(28,"Download");
            hudValList.Add(29,"Access Ct");

  
         }

 

        private void CamXPlus_Click(object sender, RoutedEventArgs e)
        {
            myPCamera.Position = new Point3D(myPCamera.Position.X + 0.5, myPCamera.Position.Y, myPCamera.Position.Z);
        }

        private void CamXMinus_Click(object sender, RoutedEventArgs e)
        {
            myPCamera.Position = new Point3D(myPCamera.Position.X - 0.5, myPCamera.Position.Y, myPCamera.Position.Z);
        }

        private void CamYPlus_Click(object sender, RoutedEventArgs e)
        {
            myPCamera.Position = new Point3D(myPCamera.Position.X, myPCamera.Position.Y + 0.5, myPCamera.Position.Z);
        }

        private void CamYMinus_Click(object sender, RoutedEventArgs e)
        {
            myPCamera.Position = new Point3D(myPCamera.Position.X, myPCamera.Position.Y - 0.5, myPCamera.Position.Z);
        }

        private void CamZPlus_Click(object sender, RoutedEventArgs e)
        {
            if (myPCamera.HasAnimatedProperties)
            {
                myPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                myPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            }
            myPCamera.Position = campos;
            myPCamera.LookDirection = camlook;
            myPCamera.Position = new Point3D(myPCamera.Position.X, myPCamera.Position.Y, myPCamera.Position.Z + 0.5);
            campos = myPCamera.Position;
        }

        private void CamZMinus_Click(object sender, RoutedEventArgs e)
        {
            if (myPCamera.HasAnimatedProperties)
            {
                myPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                myPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            }
            myPCamera.Position = campos;
            myPCamera.LookDirection = camlook;
            myPCamera.Position = new Point3D(myPCamera.Position.X, myPCamera.Position.Y, myPCamera.Position.Z - 0.5);
            campos = myPCamera.Position;

        }

        private void EyeXPlus_Click(object sender, RoutedEventArgs e)
        {
            myPCamera.LookDirection = new Vector3D(myPCamera.LookDirection.X + 0.5, myPCamera.LookDirection.Y, myPCamera.LookDirection.Z);
        }

        private void EyeYPlus_Click(object sender, RoutedEventArgs e)
        {
            //myPCamera.LookDirection = new Vector3D(myPCamera.LookDirection.X, myPCamera.LookDirection.Y, myPCamera.LookDirection.Z + 0.5);
            Point3D endpoint = new Point3D(transformGroupList[_vertices - 1].Children[0].Value.OffsetX, transformGroupList[_vertices - 1].Children[0].Value.OffsetY, transformGroupList[_vertices - 1].Children[0].Value.OffsetZ);
            if (myPCamera.HasAnimatedProperties)
            {
            myPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
            myPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            } 
            Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(-10, -5, -3), new Duration(TimeSpan.FromMilliseconds(_acrosssecs)));
            Point3DAnimation cameraPositionAnimation = new Point3DAnimation(new Point3D(10, 5, 2.8), new Duration(TimeSpan.FromMilliseconds(_acrosssecs)));
            myVectorAnimation.From = camlook;
            cameraPositionAnimation.From = campos;
            myPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, cameraPositionAnimation);
            myPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, myVectorAnimation);
            campos = new Point3D(10, 5, 2.8);
            camlook = new Vector3D(-10, -5, -3);
        }

        private void EyeZPlus_Click(object sender, RoutedEventArgs e)
        {
           //myPCamera.LookDirection = new Vector3D(myPCamera.LookDirection.X, myPCamera.LookDirection.Y, myPCamera.LookDirection.Z + 0.5);
            Point3D endpoint = new Point3D(transformGroupList[_vertices - 1].Children[0].Value.OffsetX, transformGroupList[_vertices - 1].Children[0].Value.OffsetY, transformGroupList[_vertices - 1].Children[0].Value.OffsetZ);
            if (myPCamera.HasAnimatedProperties)
            {
                myPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                myPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            } 
                Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(-8, -5, 7.8), new Duration(TimeSpan.FromMilliseconds(_acrosssecs)));
                Point3DAnimation cameraPositionAnimation = new Point3DAnimation(new Point3D(8, 5, endpoint.Z - 5), new Duration(TimeSpan.FromMilliseconds(_acrosssecs)));
            myVectorAnimation.From = camlook;
            cameraPositionAnimation.From = campos;
            myPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, cameraPositionAnimation);
            myPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, myVectorAnimation);
            campos = new Point3D(8, 5, endpoint.Z - 5);
            camlook = new Vector3D(-8, -5, 7.8);
        }

        private void EyeXMinus_Click(object sender, RoutedEventArgs e)
        {
            myPCamera.LookDirection = new Vector3D(myPCamera.LookDirection.X - 0.5, myPCamera.LookDirection.Y, myPCamera.LookDirection.Z);
            // jump a quarter of the way down the model
        }

        private void EyeYMinus_Click(object sender, RoutedEventArgs e)
        {
            //myPCamera.LookDirection = new Vector3D(myPCamera.LookDirection.X, myPCamera.LookDirection.Y - 0.5, myPCamera.LookDirection.Z);
            //if (myPCamera.HasAnimatedProperties)
            //{
                myPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                myPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            //}
                Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(10, -5, -5.8), new Duration(TimeSpan.FromMilliseconds(_endsecs)));
                Point3DAnimation cameraPositionAnimation = new Point3DAnimation(new Point3D(-10, 5, 3), new Duration(TimeSpan.FromMilliseconds(_endsecs)));
            myVectorAnimation.From = camlook;
            cameraPositionAnimation.From = campos;
            myPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, cameraPositionAnimation);
            myPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, myVectorAnimation);
            campos = new Point3D(-10, 5, 3);
            camlook = new Vector3D(10, -5, -5.8); 
        }

        private void EndTravel_Click(object sender, RoutedEventArgs e)
        {
            ////myPCamera.LookDirection = new Vector3D(myPCamera.LookDirection.X, myPCamera.LookDirection.Y, myPCamera.LookDirection.Z - 0.5);
             
            // move camera to last grid and adjust look direction
            // todo: focus on start of grid instead of last vertex
            Point3D endpoint = new Point3D(transformGroupList[_vertices - 1].Children[0].Value.OffsetX, transformGroupList[_vertices - 1].Children[0].Value.OffsetY, transformGroupList[_vertices - 1].Children[0].Value.OffsetZ);
            //if (myPCamera.HasAnimatedProperties)
            //{
                myPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                myPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            //} 
            Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(10,-5,5.8), new Duration(TimeSpan.FromMilliseconds(_endsecs)));
            Point3DAnimation cameraPositionAnimation = new Point3DAnimation(new Point3D(endpoint.X - 10, endpoint.Y + 5, endpoint.Z + 5.8 - 10), new Duration(TimeSpan.FromMilliseconds(_endsecs)));
            
            myVectorAnimation.From = camlook;
            cameraPositionAnimation.From = campos; 
            myPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, cameraPositionAnimation);
            myPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, myVectorAnimation);
            campos = new Point3D(endpoint.X - 10, endpoint.Y + 5, endpoint.Z + 5.8 - 10);
            camlook = new Vector3D(10, -5, 5.8); 
        }
  

        private void RotXPlus_Click(object sender, RoutedEventArgs e)
        {

            for (int i = 0; i < modelGroupList.Count; i++)
            {
                transformGroupList[i].Children[1] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), ((AxisAngleRotation3D)((RotateTransform3D)transformGroupList[i].Children[1]).Rotation).Angle + 5));
            }
        }

        private void RotXMinus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGroupList.Count; i++)
            {
                transformGroupList[i].Children[1] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), ((AxisAngleRotation3D)((RotateTransform3D)transformGroupList[i].Children[1]).Rotation).Angle - 5));
            }
        }

        private void RotYPlus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGroupList.Count; i++)
            {
                transformGroupList[i].Children[2] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), ((AxisAngleRotation3D)((RotateTransform3D)transformGroupList[i].Children[2]).Rotation).Angle + 5));
            }
        }
        private void RotYMinus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGroupList.Count; i++)
            {
                transformGroupList[i].Children[2] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), ((AxisAngleRotation3D)((RotateTransform3D)transformGroupList[i].Children[2]).Rotation).Angle - 5));
            }
        }

        private void RotZPlus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGroupList.Count; i++)
            {
                transformGroupList[i].Children[3] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), ((AxisAngleRotation3D)((RotateTransform3D)transformGroupList[i].Children[3]).Rotation).Angle + 15));
            }

        }

        private void RotZMinus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGroupList.Count; i++)
            {
                transformGroupList[i].Children[3] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), ((AxisAngleRotation3D)((RotateTransform3D)transformGroupList[i].Children[3]).Rotation).Angle - 15));
            }

        }

 

        private void GetDataBtn_Click(object sender, RoutedEventArgs e)
        {
            Button buttonThis = (Button)sender; 
            string buttonCommand = buttonThis.Tag.ToString();
                
                switch (buttonCommand)
                {
                    case "galaxy":
                        startDate = GalaxyStartDatePicker.SelectedDate;
                        endDate = GalaxyEndDatePicker.SelectedDate;
                        view = 0;
                        GetDataSet(view);
                        if (dataSetArray.Count > 0)
                        {
                            GalaxywrapPanel1.Children.Clear();
                            DrawGalaxyModel();
                        }
                        else
                        {
                             //indicate no results returned
                        }
                        break;
                    case "galaxy_id":
                        startDate = GalaxyStartDatePicker.SelectedDate;
                        endDate = GalaxyEndDatePicker.SelectedDate;
                        view = 1;
                        GetDataSet(view);
                        if (dataSetArray.Count > 0)
                        {
                            DrawGalaxyModel();
                        }
                        else
                        {
                            //indicate no results returned
                        }
                        break;
                    default:
                        startDate = startDatePicker.SelectedDate;
                        endDate = endDatePicker.SelectedDate;
                        view = 0;
                        GetDataSet(view);
                        if (dataSetArray.Count > 0)
                        {
                            wrapPanel1.Children.Clear(); 
                            DrawSomeModels();
                        }
                        else
                        {
                             //indicate no results returned
                        } 
                        break;
                }
           

            buttonCommand = null;
            buttonThis = null;
        }

        private void GetIdDataBtn_Click(object sender, RoutedEventArgs e)
        {
            startDate = startDatePicker.SelectedDate;
            endDate = endDatePicker.SelectedDate;            
            if (SearchParamBox.Text != "") { _workerId = SearchParamBox.Text; }
            view = 1;
            GetDataSet(view);
            SearchParamBox.Text = "";
            // TODO: re-factor
            if (dataSetArray.Count > 0)
            {
                Button buttonThis = (Button)sender;
                switch (buttonThis.Tag.ToString())
                {
                    case "galaxy":
                        DrawGalaxyModel();
                        break;
                    default:
                        DrawSomeModels();
                        break;
                }
            }
            else
            {
                //indicate no results returned
            }
        }

        private void startDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            endDatePicker.SelectedDate = startDatePicker.SelectedDate.Value.AddDays(1);
        }
        private void GalaxyStartDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            GalaxyEndDatePicker.SelectedDate = GalaxyStartDatePicker.SelectedDate.Value.AddDays(1);
        }


        public void OnHudTextMouseDown(object sender, System.Windows.Input.MouseEventArgs e)
        {
            tabItem2.IsSelected = true;
            argsPq.Clear();
            argsPq.Add("id",_workerId);
            statusLabel.Clear();
            statusLabel.Text = getWorkerData();
            argsPq.Add("email", _email);

            CaseLabel1.Content = "";
            CaseLabel2.Content = _workerId;
            tabItem3.IsSelected = true;
        }

  
        private void label1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Feature disabled in demo version");

                       
        }


        private string getWorkerData()
        {

            string _connection = "Data Source=" + ConfigurationManager.AppSettings["Database"].ToString() +";Database='Demo_ProI_Reporting';" +
          "Persist Security Info=False;Integrated Security=SSPI; Connect Timeout=120;Min Pool Size = 10; Max Pool Size = 100";
            SqlConnection connection = new SqlConnection(_connection);
            SqlCommand _cmdDatabase = new SqlCommand();
            _cmdDatabase.Connection = connection;
            _cmdDatabase.CommandType = CommandType.StoredProcedure;
            _cmdDatabase.CommandText = "[dbo].[Employee_Drilldown_v2]";
            _cmdDatabase.Parameters.AddWithValue("worker_id", Int32.Parse(argsPq["id"]));
            _cmdDatabase.CommandTimeout = 120;
            connection.Open();
            SqlDataReader reader = _cmdDatabase.ExecuteReader();
            string workerText = "";
            while (reader.Read())
            {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    workerText += reader[i] + "\n";
                }
                _email = reader[3].ToString();
            }
            connection.Close();
            return workerText;
        }

        private void PasteFromStatus_Click(object sender, RoutedEventArgs e)
        {
            NotesBox.Text += statusLabel.Text;
        }

        private void label3_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            MessageBox.Show("Feature disabled in demo version");
        }

        private void CaseButton_Click(object sender, RoutedEventArgs e)
        {
         }

  

        private void caseDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
  
        }

        private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(ConfigurationManager.AppSettings["DrilldownDirectory"].ToString());
            List<string> fileinfo = new List<string>();
            foreach (FileInfo fi in di.GetFiles())
            {
                fileinfo.Add(fi.Name);
            }
            DrilldownGrid.ItemsSource = fileinfo;

        }

        private void DrilldownGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            StreamReader sr = new StreamReader(ConfigurationManager.AppSettings["DrilldownDirectory"].ToString() + DrilldownGrid.SelectedItem.ToString());
            int len = 0;
            var records = new ObservableCollection<Record>();
            while (!sr.EndOfStream)
            {
                string[] firstline = sr.ReadLine().Split(',');
                 Record record = new Record();
                for (int i = 0; i < firstline.Length; i++)
                {                   
                   
                    record.Properties.Add(new Property(i.ToString(),firstline[i]));
                }
                records.Add(record);
            }

            var columns = records[0].Properties;
            for (int i=0; i < columns.Count-1; i++)
            {
                var binding = new Binding(string.Format("Properties[{0}].Value", i));    
                 drilldownDataGrid.Columns.Add(new DataGridTextColumn() { Header = records[0].Properties[i].Value, Binding = binding });
            }
                drilldownDataGrid.ItemsSource = records;

        }

        //galaxy tab methods and functions

        private void DrawGalaxyModel()
        {
             _series = _xunits-1;
            if (GalaxyViewGrid.Children.Count > 1)
            {
                GalaxyViewGrid.Children.Remove(GalaxyViewport);
            }
            GalaxyViewport = new Viewport3D();
            GalaxyViewport.Name = "GalaxyViewport";
            allGalaxyModels = new Model3DGroup();
            ModelVisual3D GalaxyModelVisual = new ModelVisual3D();
            transformGalaxyGroupList = new List<Transform3DGroup>();
            modelGalaxyGroupList = new List<Model3DGroup>();
            modelGalaxyUIList = new List<ModelUIElement3D>();

            //Define lights and cameras
            GalaxyPCamera.FarPlaneDistance = 100;
            GalaxyPCamera.NearPlaneDistance = 0;
            GalaxyPCamera.FieldOfView = 45;
            GalaxyPCamera.Position =  new Point3D(-25, 1, -20);
            Point3D objectPosition = new Point3D(0, 0, 0);
            GalaxyPCamera.LookDirection = new Vector3D(objectPosition.X - GalaxyPCamera.Position.X, objectPosition.Y - GalaxyPCamera.Position.Y, objectPosition.Z - GalaxyPCamera.Position.Z+5);
            galcampos = GalaxyPCamera.Position;
            galcamlook = GalaxyPCamera.LookDirection;

            GalaxyPCamera.UpDirection = new Vector3D(0, 0.2, 0);

            GalaxyDLight.Color = Colors.White;
            GalaxyDLight.Direction = new Vector3D(-3, -4, -5);

            GalaxyAmbLight.Color = Colors.White;

            //set Geometry property of MeshGeometry3D
            side1.Geometry = meshGeoList[0];
            side2.Geometry = meshGeoList[1];
            side3.Geometry = meshGeoList[2];
            side4.Geometry = meshGeoList[3];
            side5.Geometry = meshGeoList[4];
            side6.Geometry = meshGeoList[5];
            side7.Geometry = meshGeoList[0];
            side8.Geometry = meshGeoList[1];
            side9.Geometry = meshGeoList[2];
            side10.Geometry = meshGeoList[3];
            side11.Geometry = meshGeoList[4];
            side12.Geometry = meshGeoList[5];
            side13.Geometry = meshGeoList[0];
            side14.Geometry = meshGeoList[1];
            side15.Geometry = meshGeoList[2];
            side16.Geometry = meshGeoList[3];
            side17.Geometry = meshGeoList[4];
            side18.Geometry = meshGeoList[5];
            side19.Geometry = meshGeoList[0];
            side20.Geometry = meshGeoList[1];
            side21.Geometry = meshGeoList[2];
            side22.Geometry = meshGeoList[3];
            side23.Geometry = meshGeoList[4];
            side24.Geometry = meshGeoList[5];

            try
            {
                // create model3dgroups
                for (int i = 0; i < _zunits * _series  ; i++)
                {
                    modelGalaxyGroupList.Add(new Model3DGroup());
                    transformGalaxyGroupList.Add(new Transform3DGroup());
                    modelGalaxyUIList.Add(new ModelUIElement3D());
                }
            }
            catch (Exception ex)
            {


            }
            //create translations
            //try
           // {
            for (int i = 0; i < _zunits * _series; i++)
                {
                    double rho = dataSetArray[i / _series][i % _series + 1];//1,2,3,4
                    double phi = System.Math.PI / 4 * dataSetArray[i / _series][i % _series + 1 + 2 * _xunits];//13,14,15,16
                    double theta = System.Math.PI / 8 * dataSetArray[i / _series][i % _series + 1 + 1 * _xunits];//7,8,9,10
                    transformGalaxyGroupList[i].Children.Add(new TranslateTransform3D(rho * System.Math.Sin(1-phi) * System.Math.Cos(1-theta)
                                                                                       , rho * System.Math.Sin(1-phi) * System.Math.Sin(1-theta)
                                                                                       , rho * System.Math.Cos(1-phi)));
                    //transformGalaxyGroupList[i].Children.Add(new TranslateTransform3D(dataSetArray[i][2]
                    //                                               , dataSetArray[i][12]
                    //                                               ,  dataSetArray[i][7]));
                    transformGalaxyGroupList[i].Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 0)));
                    transformGalaxyGroupList[i].Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0)));
                    transformGalaxyGroupList[i].Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 0)));
                    transformGalaxyGroupList[i].Children.Add(new ScaleTransform3D(1.0, 1.0, 1.0));

                }
            //}
            //catch (Exception ex)
            //{
             //   Console.WriteLine(ex.StackTrace);
            //}

            //Add transformation to the model

           // try
           // {
                for (int i = 0; i < _zunits*_series; i++)
                {
                    AddSidesToGalaxyGroup(i);
                    modelGalaxyUIList[i].Model = modelGalaxyGroupList[i];
                    modelGalaxyUIList[i].Transform = transformGalaxyGroupList[i];
                    modelGalaxyUIList[i].MouseLeftButtonDown += new MouseButtonEventHandler(OnGalaxyModelMouseDown);
                    //modelGalaxyUIList[i].MouseEnter += new MouseEventHandler(OnModelMouseOver);
                    allGalaxyModels.Children.Add(modelGalaxyGroupList[i]);
                }
           // }
            //catch (Exception ex)
            //{

           // }

            for (int i = 0; i < modelGalaxyUIList.Count; i++)
            {
                GalaxyViewport.Children.Add(modelGalaxyUIList[i]);
            }
            allGalaxyModels.Children.Add(GalaxyAmbLight);
            GalaxyRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 0), 0));
            allGalaxyModels.Transform = GalaxyRotateTransform;


            GalaxyViewport.Camera = GalaxyPCamera;
            GalaxyModelVisual.Content = allGalaxyModels;
            //myModelVisual.Content = null;

            GalaxyViewport.Children.Add(GalaxyModelVisual);

            //mainWindow.Content = myViewport;
            GalaxyViewGrid.Children.Add(GalaxyViewport);


        }


        private void AddSidesToGalaxyGroup(int idx)
        {
            //logic to decide which color sides to add
            //dataSetArray[i%(_xunits,_yunits)]
            //Random rand = new Random();
            //int color = rand.Next(1, 4);
            //int color = cubemodel
            //double dataItem = dataSetArray[idx / (_xunits * _yunits)][idx % (_xunits * _yunits)];
            if (idx%_series == 0)
            {
                // yellow box
                if (dataSetArray[idx / _series][1] > 2 | dataSetArray[idx / _series][7] > 2 | dataSetArray[idx / _series][13] > 2)
                {
                    for (int i = 0; i <= _numsides - 1; i++)
                    {
                        modelGalaxyGroupList[idx].Children.Add(sideArray[i]);
                    }

                }
            }
            else if (idx % _series == 1)
            {
                //purple box
                if (dataSetArray[idx / _series][2] > 2 | dataSetArray[idx / _series][8] > 2 | dataSetArray[idx / _series][14] > 2)
                {
                    for (int i = _numsides * 2; i <= _numsides * 3 - 1; i++)
                    {
                        modelGalaxyGroupList[idx].Children.Add(sideArray[i]);
                    }
                }
            }
            else if (idx % _series == 2)
            {
                //red box
                if (dataSetArray[idx / _series][3] > 2 | dataSetArray[idx / _series][9] > 2 | dataSetArray[idx / _series][15] > 2)
                {
                    for (int i = _numsides; i <= _numsides * 2 - 1; i++)
                    {
                        modelGalaxyGroupList[idx].Children.Add(sideArray[i]);
                    }
                }
            }
            else if (idx % _series == 3)
            {
                
                if (dataSetArray[idx / _series][4] > 2 | dataSetArray[idx / _series][10] > 2 | dataSetArray[idx / _series][16] > 2)
                {
                    //blue box
                    for (int i = _numsides * 3; i <= _numsides * 4 - 1; i++)
                    {
                        modelGalaxyGroupList[idx].Children.Add(sideArray[i]);
                    }
                }
            }          
 
            //else if (dataItem <= 3.0 & dataItem > 1.0)
            //{
            //    // yellow box
            //    for (int i = 0; i <= _numsides - 1; i++)
            //    {
            //        modelGroupList[idx].Children.Add(sideArray[i]);
            //    }
            //}
            //blue box
            //for (int i = _numsides * 3; i <= _numsides * 4 - 1; i++)
            //{
            //    modelGalaxyGroupList[idx].Children.Add(sideArray[i]);
            //}
            //else
            //{
            //    //for (int i = _numsides * 2; i <= _numsides * 3 - 1; i++)
            //    //{
            //    //    modelGroupList[idx].Children.Add(sideArray[i]);
            //    //}

            //}

        }


        private void GalaxyCamXPlus_Click(object sender, RoutedEventArgs e)
        {
            if (GalaxyPCamera.HasAnimatedProperties)
            {
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            }
            GalaxyPCamera.Position = galcampos;
            GalaxyPCamera.LookDirection = galcamlook;
            GalaxyPCamera.Position = new Point3D(GalaxyPCamera.Position.X + 0.5, GalaxyPCamera.Position.Y, GalaxyPCamera.Position.Z);
            galcampos = GalaxyPCamera.Position;
            galcamlook = GalaxyPCamera.LookDirection;
        }

        private void GalaxyCamXMinus_Click(object sender, RoutedEventArgs e)
        {
            if (GalaxyPCamera.HasAnimatedProperties)
            {
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            }
            GalaxyPCamera.Position = galcampos;
            GalaxyPCamera.LookDirection = galcamlook; 
            GalaxyPCamera.Position = new Point3D(GalaxyPCamera.Position.X - 0.5, GalaxyPCamera.Position.Y, GalaxyPCamera.Position.Z);
            galcampos = GalaxyPCamera.Position;
            galcamlook = GalaxyPCamera.LookDirection;
        }

        private void GalaxyCamYPlus_Click(object sender, RoutedEventArgs e)
        {
            if (GalaxyPCamera.HasAnimatedProperties)
            {
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            }
            GalaxyPCamera.Position = galcampos;
            GalaxyPCamera.LookDirection = galcamlook; 
            GalaxyPCamera.Position = new Point3D(GalaxyPCamera.Position.X, GalaxyPCamera.Position.Y + 0.5, GalaxyPCamera.Position.Z);
            galcampos = GalaxyPCamera.Position;
            galcamlook = GalaxyPCamera.LookDirection;
        }

        private void GalaxyCamYMinus_Click(object sender, RoutedEventArgs e)
        {
            if (GalaxyPCamera.HasAnimatedProperties)
            {
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            }
            GalaxyPCamera.Position = galcampos;
            GalaxyPCamera.LookDirection = galcamlook; 
            GalaxyPCamera.Position = new Point3D(GalaxyPCamera.Position.X, GalaxyPCamera.Position.Y - 0.5, GalaxyPCamera.Position.Z);
            galcampos = GalaxyPCamera.Position;
            galcamlook = GalaxyPCamera.LookDirection;
        }

        private void GalaxyCamZPlus_Click(object sender, RoutedEventArgs e)
        {
            if (GalaxyPCamera.HasAnimatedProperties)
            {
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            }
            GalaxyPCamera.Position = galcampos;
            GalaxyPCamera.LookDirection = galcamlook; 
            GalaxyPCamera.Position = new Point3D(GalaxyPCamera.Position.X, GalaxyPCamera.Position.Y, GalaxyPCamera.Position.Z + 0.5);
            galcampos = GalaxyPCamera.Position;
            galcamlook = GalaxyPCamera.LookDirection;
        }

        private void GalaxyCamZMinus_Click(object sender, RoutedEventArgs e)
        {
            if (GalaxyPCamera.HasAnimatedProperties)
            {
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
                GalaxyPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            }
            GalaxyPCamera.Position = galcampos;
            GalaxyPCamera.LookDirection = galcamlook; 
            GalaxyPCamera.Position = new Point3D(GalaxyPCamera.Position.X, GalaxyPCamera.Position.Y, GalaxyPCamera.Position.Z - 0.5);
            galcampos = GalaxyPCamera.Position;
            galcamlook = GalaxyPCamera.LookDirection;
        }

        private void GalaxyEyeXPlus_Click(object sender, RoutedEventArgs e)
        {
            GalaxyPCamera.LookDirection = new Vector3D(GalaxyPCamera.LookDirection.X + 0.5, GalaxyPCamera.LookDirection.Y, GalaxyPCamera.LookDirection.Z);
        }

        private void GalaxyEyeYPlus_Click(object sender, RoutedEventArgs e)
        {
            GalaxyPCamera.LookDirection = new Vector3D(GalaxyPCamera.LookDirection.X, GalaxyPCamera.LookDirection.Y + 0.5, GalaxyPCamera.LookDirection.Z);
        }

        private void GalaxyEyeZPlus_Click(object sender, RoutedEventArgs e)
        {
            GalaxyPCamera.LookDirection = new Vector3D(GalaxyPCamera.LookDirection.X, GalaxyPCamera.LookDirection.Y, GalaxyPCamera.LookDirection.Z + 0.5);
        }

        private void GalaxyEyeXMinus_Click(object sender, RoutedEventArgs e)
        {
            GalaxyPCamera.LookDirection = new Vector3D(GalaxyPCamera.LookDirection.X - 0.5, GalaxyPCamera.LookDirection.Y, GalaxyPCamera.LookDirection.Z);
        }

        private void GalaxyEyeYMinus_Click(object sender, RoutedEventArgs e)
        {
            GalaxyPCamera.LookDirection = new Vector3D(GalaxyPCamera.LookDirection.X, GalaxyPCamera.LookDirection.Y - 0.5, GalaxyPCamera.LookDirection.Z);
        }

        private void GalaxyEyeZMinus_Click(object sender, RoutedEventArgs e)
        {
            //GalaxyPCamera.LookDirection = new Vector3D(GalaxyPCamera.LookDirection.X, GalaxyPCamera.LookDirection.Y, GalaxyPCamera.LookDirection.Z - 0.5);
            Point3D endpoint = new Point3D(0.0, 0.0, 0.0);
            //if (myPCamera.HasAnimatedProperties)
            //{
            GalaxyPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, null);
            GalaxyPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, null);
            //} 
            Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(-25, 1, 10), new Duration(TimeSpan.FromMilliseconds(_endsecs)));
            Point3DAnimation cameraPositionAnimation = new Point3DAnimation(new Point3D(25, 1, -10), new Duration(TimeSpan.FromMilliseconds(_endsecs)));

            myVectorAnimation.From = galcamlook;
            cameraPositionAnimation.From = galcampos;
            GalaxyPCamera.BeginAnimation(PerspectiveCamera.PositionProperty, cameraPositionAnimation);
            GalaxyPCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, myVectorAnimation);
            galcampos = new Point3D(25, 1, 10);
            galcamlook = new Vector3D(-25, 1, -10); 
        }

             

        private void GalaxyRotXPlus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGalaxyGroupList.Count; i++)
            {
                transformGalaxyGroupList[i].Children[1] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), ((AxisAngleRotation3D)((RotateTransform3D)transformGalaxyGroupList[i].Children[1]).Rotation).Angle + 5));
            }
        }

        private void GalaxyRotXMinus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGalaxyGroupList.Count; i++)
            {
                transformGalaxyGroupList[i].Children[1] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), ((AxisAngleRotation3D)((RotateTransform3D)transformGalaxyGroupList[i].Children[1]).Rotation).Angle - 5));
            }
        }

        private void GalaxyRotYPlus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGalaxyGroupList.Count; i++)
            {
                transformGalaxyGroupList[i].Children[2] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), ((AxisAngleRotation3D)((RotateTransform3D)transformGalaxyGroupList[i].Children[2]).Rotation).Angle + 5));
            }
        }
        private void GalaxyRotYMinus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGalaxyGroupList.Count; i++)
            {
                transformGalaxyGroupList[i].Children[2] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), ((AxisAngleRotation3D)((RotateTransform3D)transformGalaxyGroupList[i].Children[2]).Rotation).Angle - 15));
            }
        }

        private void GalaxyRotZPlus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGalaxyGroupList.Count; i++)
            {
                transformGalaxyGroupList[i].Children[3] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), ((AxisAngleRotation3D)((RotateTransform3D)transformGalaxyGroupList[i].Children[3]).Rotation).Angle + 15));
            }
        }

        private void GalaxyRotZMinus_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < modelGalaxyGroupList.Count; i++)
            {
                transformGalaxyGroupList[i].Children[3] = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), ((AxisAngleRotation3D)((RotateTransform3D)transformGalaxyGroupList[i].Children[3]).Rotation).Angle - 5));
            }

        }

        private void GalaxyNearPlus_Click(object sender, RoutedEventArgs e)
        {
            GalaxyPCamera.NearPlaneDistance += 1.0;
        }
        private void GalaxyNearMinus_Click(object sender, RoutedEventArgs e)
        {
            GalaxyPCamera.NearPlaneDistance -= 1.0;
        }
        private void GalaxyFarPlus_Click(object sender, RoutedEventArgs e)
        {
            GalaxyPCamera.FarPlaneDistance += 1.0;
        }
        private void GalaxyFarMinus_Click(object sender, RoutedEventArgs e)
        {
            GalaxyPCamera.FarPlaneDistance -= 1.0;
        }

        /// <summary>
        /// On mouse click, return data on the specific model
        /// where the click happened.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void OnGalaxyModelMouseDown(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // get ID from the model
            idx = (GalaxyViewport.Children.IndexOf((ModelUIElement3D)sender));

            if (view == 0)
            {
                _workerId = dataSetArray[idx/_series][0].ToString();

            }
            if (view == 1)
            {
                startDatePicker.SelectedDate = Convert.ToDateTime(dataSetArray[idx / _series][0].ToString().Substring(4, 2) + "/" + dataSetArray[idx / _series][0].ToString().Substring(6, 2) + "/" + dataSetArray[idx / _series][0].ToString().Substring(0, 4));
            }
            #region hudpanel
            // call up HUD in panel
            GalaxywrapPanel1.Children.Clear();
            int _labelwidth = 70;
            //headers
            TextBox lbl = new TextBox();
            lbl.Text = "Worker";
            lbl.Width = _labelwidth;
            lbl.FontWeight = FontWeights.ExtraBold;
            GalaxywrapPanel1.Children.Add(lbl);
            TextBox lbl0 = new TextBox();
            lbl0.Text = "IP1"; //
            lbl0.Width = _labelwidth;
            lbl0.FontWeight = FontWeights.ExtraBold;
            lbl0.Background = Brushes.Yellow;
            GalaxywrapPanel1.Children.Add(lbl0);
            TextBox lbl1 = new TextBox();
            lbl1.Text = "IP2"; //
            lbl1.Width = _labelwidth;
            lbl1.FontWeight = FontWeights.ExtraBold;
            lbl1.Background = Brushes.MediumPurple;
            GalaxywrapPanel1.Children.Add(lbl1);
            TextBox lbl2 = new TextBox();
            lbl2.Text = "IP3"; //
            lbl2.Width = _labelwidth;
            lbl2.FontWeight = FontWeights.ExtraBold;
            lbl2.Background = Brushes.Red;
            GalaxywrapPanel1.Children.Add(lbl2);
            TextBox lbl3 = new TextBox();
            lbl3.Text = "VPN";
            lbl3.Width = _labelwidth;
            lbl3.FontWeight = FontWeights.ExtraBold;
            lbl3.Background = Brushes.Blue;
            GalaxywrapPanel1.Children.Add(lbl3);
            TextBox lbl4 = new TextBox();
            lbl4.Text = "AD";
            lbl4.Width = _labelwidth;
            lbl4.FontWeight = FontWeights.ExtraBold;
            textboxList = new List<TextBox>();
            labelList = new List<TextBox>();
            GalaxywrapPanel1.Children.Add(lbl4);

            for (int i = 0; i < hudMapList.Count - 5; i += _xunits)
            {
                for (int j = 0; j < _xunits; j++)
                {
                    labelList.Add(new TextBox());
                    labelList[i + j].Text = hudMapList[i + j];
                    labelList[i + j].Width = _labelwidth;
                    if (dataSetArray[idx / _series][hudMapList.Count - _xunits - i + j] > 10000000.0)
                    {
                        labelList[i + j].BorderThickness = new Thickness(2.0);
                        labelList[i + j].BorderBrush = Brushes.Blue;
                    }
                    else if (dataSetArray[idx / _series][hudMapList.Count - _xunits - i + j] > 3.0)
                    {
                        labelList[i + j].BorderThickness = new Thickness(2.0);

                        labelList[i + j].BorderBrush = Brushes.DarkRed;
                    }
                    else if (dataSetArray[idx / _series][hudMapList.Count - _xunits - i + j] > 1.0)
                    {
                        labelList[i + j].BorderThickness = new Thickness(2.0);
                        labelList[i + j].BorderBrush = Brushes.Yellow;
                    }
                    else
                    { }

                    GalaxywrapPanel1.Children.Add(labelList[i + j]);


                }
                for (int j = 0; j < _xunits; j++)
                {
                    textboxList.Add(new TextBox());
                    //if (i + j != 0)
                    //{
                    textboxList[i + j].Text = dataSetArray[idx / _series][hudMapList.Count - _xunits - i + j].ToString();
                    // }
                    textboxList[i + j].Width = _labelwidth;
                    textboxList[i + j].MouseDoubleClick += new MouseButtonEventHandler(OnHudTextMouseDown);
                    if (i >= hudMapList.Count - hudValList.Count)
                    {
                        textboxList[i + j].ToolTip += hudValList[i + j] + ":" + dataSetArray[idx / _series][i + j + hudValList.Count].ToString();
                    }
                    if (dataSetArray[idx / _series][hudMapList.Count - _xunits - i + j] > 10000000.0)
                    {
                        textboxList[i + j].BorderThickness = new Thickness(2.0);
                        textboxList[i + j].BorderBrush = Brushes.Blue;
                    }
                    else if (dataSetArray[idx / _series][hudMapList.Count - _xunits - i + j] > 3.0)
                    {
                        textboxList[i + j].BorderThickness = new Thickness(2.0);
                        textboxList[i + j].BorderBrush = Brushes.DarkRed;
                    }
                    else if (dataSetArray[idx / _series][hudMapList.Count - _xunits - i + j] > 1.0)
                    {
                        textboxList[i + j].BorderThickness = new Thickness(2.0);
                        textboxList[i + j].BorderBrush = Brushes.Yellow;
                    }
                    else
                    { }
                    GalaxywrapPanel1.Children.Add(textboxList[i + j]);

                }
            }

            #endregion
            startDate = GalaxyStartDatePicker.SelectedDate;
            endDate = GalaxyEndDatePicker.SelectedDate;
        }


        private void grid2_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                ModelUIElement3D pushed = (ModelUIElement3D)sender;
                pushpinGroupList.Add(new Model3DGroup());
                pushpinTransformGroupList.Add(new Transform3DGroup());
                pushpinUIList.Add(new ModelUIElement3D());
                int pushidx = pushpinGroupList.Count - 1;
                //pushpinTransformGroupList[pushidx].Children.Add(new TranslateTransform3D(new Vector3D(point.X + 5, point.Y + 5, point.X + 2.0)));
                pushpinTransformGroupList[pushidx].Children.Add(new TranslateTransform3D(new Vector3D(-0.5,0.0,pushed.Transform.Value.OffsetZ)));
                // yellow box
                for (int i = _numsides * 0; i <= _numsides * 1 -1; i++)
                {
                    pushpinGroupList[pushidx].Children.Add(sideArray[i]);
                }
                pushpinUIList[pushidx].Model = pushpinGroupList[pushidx];
                pushpinUIList[pushidx].Transform = pushpinTransformGroupList[pushidx];
                pushpinUIList[pushidx].MouseLeftButtonDown += new MouseButtonEventHandler(OnPushpinMouseDown);
                //pushpinUIList[pushidx].MouseEnter += new MouseEventHandler(OnPushPinMouseOver);
                pushpinModels.Children.Add(pushpinGroupList[pushidx]);
                //allModels.Children.Add(pushpinGroupList[pushidx]);
                myViewport.Children.Add(pushpinUIList[pushidx]);
                pushpinVisual.Content = pushpinModels;
                myViewport.Children.Remove(pushpinVisual);
                myViewport.Children.Add(pushpinVisual);
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine(ex.Message);
            }

        }


        private void OnPushpinMouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                ModelUIElement3D pushed = (ModelUIElement3D)sender;
                int pushidx = pushpinUIList.IndexOf(pushed);
             
                ////blue box, todo: make green
                pushpinGroupList[pushidx].Children.Clear();
                for (int i = _numsides * 4; i <= _numsides * 5 - 1; i++)
                {
                    pushpinGroupList[pushidx].Children.Add(sideArray[i]);
                }
                 myViewport.Children.Remove(pushpinVisual);
                myViewport.Children.Add(pushpinVisual);
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine(ex.Message);
            }

        }


    }

    public class Property //: INotifyPropertyChanged 
    { 
        public Property(string name, object value) 
        {
            Name = name; 
            Value = value; 
        } 
        public string Name { get; private set; } 
        public object Value { get; set; } 
    }

    public class Record 
    { 
        private readonly ObservableCollection<Property> properties = new ObservableCollection<Property>(); 
        public Record(params Property[] properties) 
        { 
            foreach (var property in properties)            
                Properties.Add(property); 
        } 
        public ObservableCollection<Property> Properties { get { return properties; } } 
    }
}